"use strict";

/**
 * 通用腳本
 */
var isMobile = /Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) || /AppleWebKit.*Mobile/i.test(navigator.userAgent) || /MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent) || window.innerWidth <= 768; // 已登入 用戶菜單切換

var $userlogined = document.getElementById('j-user-logined');

if ($userlogined) {
  $userlogined.addEventListener('mouseover', function () {
    this.classList.add('active');
  });
  $userlogined.addEventListener('mouseout', function () {
    this.classList.remove('active');
  });
} // 置頂浮動菜單


var $fixedMenu = document.getElementById('j-fixed-menu');

if ($fixedMenu) {
  var $fixedMenuButton = $fixedMenu.querySelector('button');
  $fixedMenuButton.addEventListener('click', function (e) {
    $fixedMenu.classList.toggle('active');
  }, false);
  $fixedMenuButton.addEventListener('mouseover', function () {
    this.innerHTML = !$fixedMenu.classList.contains('active') ? this.getAttribute('hover') : this.getAttribute('active');
  }, false);
  $fixedMenuButton.addEventListener('mouseout', function () {
    this.innerHTML = this.getAttribute('default');
  }, false);
}

var stopTouchMove = function stopTouchMove(e) {
  e.preventDefault();
};

var onMobileControlToggle = function onMobileControlToggle(e) {
  $mobileFixed.toggleClass('show');
  $body.toggleClass('hide');

  if ($mobileFixed.hasClass('show')) {
    var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
    $mobileFixed.css('top', scrollTop + 'px');
    document.addEventListener('touchmove', stopTouchMove, {
      passive: false
    });
  } else {
    onMobileControlClose(e);
  }
};

var onMobileControlClose = function onMobileControlClose(e) {
  $mobileFixed.removeClass('show');
  $body.removeClass('hide');
  document.removeEventListener('touchmove', stopTouchMove, {
    passive: false
  });
};

var $body = $(document.body);
var $mobileControl = $('#j-mobile-control').on('click', onMobileControlToggle);
var $mobileControlClose = $('#j-mobile-control-close').on('click', onMobileControlClose);
var $mobileUser = $('#j-mobile-user').on('click', onMobileControlToggle);
var $mobileFixed = $('#j-mobile-fixed');
var $mobileNav = $('#j-mobile-nav').on('click', 'li', function () {
  var $currentNav = $mobileNav.children('.mobile-nav-item.active');
  $(this).toggleClass('active');
  $currentNav && $currentNav.removeClass('active');
}).on('click', 'a', function (e) {
  e.stopPropagation();
});
$('#j-mobile-fixed-shade').on('click', onMobileControlClose);